package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;


public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicationManager manager) {
		super(manager);
		
	}

	public void openContactPage() {
		click(By.linkText("add new"));
	}
	
	public void fillContactForm(ContactData kontakt) {
		if (kontakt.lastName == null)
		{
			kontakt.lastName = " ";
		}
		
		if (kontakt.address == null)
		{
			kontakt.address = " ";
		}
		
		if (kontakt.mobile == null)
		{
			kontakt.mobile = " ";
		}
		
		if (kontakt.email == null)
		{
			kontakt.email = " ";
		}
		
		if (kontakt.bMonth == null)
		{
			kontakt.bMonth = "-";
		}
		if (kontakt.bDay == null)
		{
			kontakt.bDay = "-";
		}
		type(By.name("firstname"),kontakt.firstName);
		type(By.name("lastname"),kontakt.lastName);
		type(By.name("address"),kontakt.address);
		type(By.name("mobile"),kontakt.mobile);
		type(By.name("email"),kontakt.email);
		
		selectByText(By.name("bday"), kontakt.bDay);
		selectByText(By.name("bmonth"), kontakt.bMonth);
			    
	    
	    
	    type(By.name("byear"),kontakt.bYear);
	    	    
	}

	
	public void submitContactCreation() {
	click(By.name("submit"));
	}

	public void returnToHome() {
	
	click(By.cssSelector("img[alt=\"Addressbook\"]"));
	}

	public void gotoContactEditPage(int index) {
		
		click(By.cssSelector("img[alt=\"Edit\"]"));
		
		
	}

public List<ContactData> getContacts() {
		
		List<ContactData> contacts = new ArrayList<ContactData>();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			ContactData contact = new ContactData();
			String title = checkbox.getAttribute("title");
			 
			contact.lastName = title.substring("Select (".length(), title.length()-")".length());
			int probelIndex = contact.lastName.indexOf(" ");
			contact.lastName = contact.lastName.substring(probelIndex + 1, contact.lastName.length());
			
			contacts.add(contact);
			
		}
		return contacts;
	}


public void deleteContact() {
		
		
		click(By.xpath("//input[@value = 'Delete']"));
}


public void submitContactModification() {
	
	
	click(By.xpath("//input[@value = 'Update']"));
}
}
